<?php

//TODO: Load the css and javascript only on the page where they are required

/** Add action on init */
add_action('init', 'nuke_init_function');

function nuke_init_function()
{
	if( ! is_admin())
	{
		/** add action to wp_print_styles print our styles */
		add_action('wp_print_styles', 'print_theme_styles');
		
		/** add action wp_print_scripts in head */
		add_action('wp_print_scripts', 'print_theme_head_scripts');
		
		/** add action to wp_print_styles for print our styles */
		add_action('wp_head', 'print_theme_conditinal_styles', 30);
		
		/** add action wp_footer */
		add_action('wp_footer', 'print_theme_footer_scripts');
	}
}

/** create a function for print our stylesheets */
function print_theme_styles()
{
	/** make array of stylesheets */
	$styles = array (
		'main-styles' => 'style.css',
		'ipad-css' => 'css/ipad.css',
		'responsive-css' => 'css/responsive.css',
		'mobile-css' => 'css/mobile.css',
		'scrollbar-css' => 'css/jquery.mCustomScrollbar.css',
		'cslider-css' => 'css/cslider.css',
		'fancybox-css' => 'js/fancybox/jquery.fancybox.css',
		'symptomchecker' => 'css/symptomchecker.css'
	);
	

	foreach($styles as $css=>$file)
	{
		/** register our stylesheets from array */
		wp_register_style($css, THEME_URL.'/'.$file, false, '1.0', 'screen' );
		
		/** enqueue our stylesheets */
		wp_enqueue_style($css);
	}

	/** register google web fonts stylesheet */
	wp_register_style('google-fonts', 'http://fonts.googleapis.com/css?family=Open+Sans:300,400,500,600,700,800', false, '1.0', 'screen');

	/** enqueue fonts stylesheet */
	wp_enqueue_style('google-fonts');
}


/** create a function for print our scripts */
function print_theme_head_scripts()
{ global $_webnukes;?>
	
	<script type="text/javascript">
			<?php if($symptoms = $_webnukes->get_settings('symptom_checker')): ?>
			var msg = {
					empty_gender: "<?php echo $_webnukes->kvalue($symptoms, 'empty_gender'); ?>",
					empty_tos:"<?php echo $_webnukes->kvalue($symptoms, 'empty_tos'); ?>",
					save_settings:"<?php echo $_webnukes->kvalue($symptoms, 'save_settings'); ?>",
					refresh_settings:"<?php echo $_webnukes->kvalue($symptoms, 'refresh_settings'); ?>",
					use_cookies:"<?php echo $_webnukes->kvalue($symptoms, 'use_cookies'); ?>"
					};
			<?php endif; ?>
			
			var ajax_url = "<?php echo home_url(); ?>/wp-admin/admin-ajax.php"; var mytheme_url = "<?php echo get_template_directory_uri()?>", use_cookies = <?php echo $_webnukes->kvalue($symptoms, 'cookies', 'true');?>; 
	</script>
	
	<?php
	/** make an array of scripts */
	$scripts = array (
		'jquery-cookie' => '/js/jquery.cookie.js',
		'jquery-fancydropdown' => '/js/jquery.fancydropdown.js',
		'jquery-rating' => '/js/jquery.rating.pack.js',
		'jquery-mousewheel' => '/js/jquery.mousewheel.min.js',
		'jquery-scrollbar' => '/js/jquery.mCustomScrollbar.js',
		'theme-script' => '/js/script.js',
		'fancybox-js' => '/js/fancybox/jquery.fancybox.pack.js',
		'fancybox-media' => '/js/fancybox/helpers/jquery.fancybox-media.js',
		'jquery-tweet' => '/js/jquery.tweet.js',
		'symptomchecker' => '/js/symptomchecker.js',
		
		'caroFredSel' => '/js/jquery.carouFredSel-6.2.1-packed.js',
		'touchSwipe' => '/js/jquery.touchSwipe.min.js'
	);

	if(is_front_page() || is_page())
	{
		$scripts['jquery-cslider'] = '/js/jquery.cslider.js';
		$scripts['modernizr-cslider'] = '/js/modernizr.cslider.js';
		$scripts['caroFredSel'] = '/js/jquery.carouFredSel-6.2.1-packed.js';
		$scripts['touchSwipe'] = '/js/jquery.touchSwipe.min.js';
	}
	
	if(is_singular('wpnukes_galleries') || is_tax('wpnukes_gallery'))
	{
		$scripts['jpages_js'] = '/js/jPages.js';
		$scripts['pop_bubble'] = '/js/pop_bubble.js';
	}
	
	wp_enqueue_script(array('jquery', 'jquery-effects-core', 'jquery-ui-tabs', 'jquery-ui-draggable'), array(), '', true);
	
	/** register and enqueue scripts */
	foreach($scripts as $js => $file)
	{
		wp_register_script($js, THEME_URL .$file, array(), '', true);
		wp_enqueue_script($js);
	}
	
	if(is_single() || is_singular()) wp_enqueue_script('comment-reply'); 
	
	echo $_webnukes->get_settings('general_settings', 'header_code');
}


function print_theme_conditinal_styles()
{
	global $wp_styles;
	
	/** enqueue conditional stylesheets */
	$styles = array(
		'style-ie-lt7' => array('css/style-ie-lt7.css', 'lt IE 7'),
		'style-ie7' => array('css/style-ie7.css', 'IE 7'),
		'style-ie8' => array('css/style-ie7.css', 'IE 8'),
		'style-ie-gt8' => array('css/style-ie-gt8.css', 'IE 9'),
	);

	foreach($styles as $css => $file)
	{
		/** enqueue conditional stylesheets */
		wp_enqueue_style( $css, THEME_URL.'/'.$file[0], false, '1.0', 'screen');
		$wp_styles->add_data($css, 'conditional', $file[1]);
		
		wp_print_styles($css);
	}
}

/** create a function for print our scripts */
function print_theme_footer_scripts() 
{
	global $_webnukes;
	$general_settings = get_option(THEME_PREFIX.'general_settings'); ?>
	
	<script type="text/javascript">
	jQuery(document).ready(function($) {
		<?php if(is_page_template('doctor_listing-tpl.php')): ?>
		$('input.stars').rating({
			callback: function(value, link){
				/** Stop the script execution till the response */
				if($(this).data('requestRunning')){
					return
				}
				
				$(this).data('requestRunning', true); /** Meke it true by default */
				var doc_id = $(this).parent().attr('data-id');
				var rating_type = $(this).parent().attr('data-type');
				var this_elem = $(this).parent();
				var ip = '<?php echo $_SERVER['REMOTE_ADDR']; ?>';
				var url = 'action=fw_ajax_callback&subaction=doctors_rating&type='+rating_type+'&value='+value+'&doctor_id='+doc_id+'&rip='+ip;
				$.ajax({
					url : '<?php echo home_url(); ?>/wp-admin/admin-ajax.php',
					type: 'POST',
					data: url,
					success: function(res){
						$('input.stars', this_elem).rating('disable');
					},
					complete: function(){
						$(this).data('requestRunning', false); /** When request is complete make it false to process further requrests */
					}
				});
			}
		});
		<?php endif; ?>
		<?php if(is_singular('wpnukes_galleries') || is_tax('wpnukes_gallery')): ?>
			<?php $images_per_page = $_webnukes->kvalue((array)get_option('wpnukes_gallery_settings'), 'images_per_page', 12); ?>
			var gallery = $('#wpnukes_gallery');
			
			var img_size = $('li[data-type="picture_filter"]', gallery).size();
			var vid_size = $('li[data-type="video_filter"]', gallery).size();
			if(img_size == 0 || vid_size == 0) $('.filterimagevideo').css('display', 'none');
			var is_pages = ((img_size + vid_size) > <?php echo $images_per_page; ?>) ? true : false;
			
			if(is_pages) $('.pagination').jPages({containerID : "wpnukes_gallery", minHeight: false, perPage : <?php echo $images_per_page; ?>, next : '<?php _e('Next', THEME_NAME); ?>', previous : '<?php _e('Previous', THEME_NAME); ?>'});
			
			$('.filterimagevideo > a').click(function(e){
				e.preventDefault();
				
				var Class = $(this).attr('class');
				$(gallery).children().removeClass('jp-hidden');
				
				if(Class == 'all') $(gallery).children().css('display', 'block');
				else
				{
					$('li[data-type="'+Class+'"]', gallery).css('display','block');
					$(gallery).children().not('[data-type="'+Class+'"]').css('display','none');
				}
				if(is_pages) $('.pagination').jPages({containerID : "wpnukes_gallery", minHeight : false, perPage : <?php echo $images_per_page; ?>, next : '<?php _e('Next', THEME_NAME); ?>', previous : '<?php _e('Previous', THEME_NAME); ?>'});
			});
		<?php endif; ?>
		$("#subgalleries").mCustomScrollbar({
			horizontalScroll:true,
			scrollInertia:0,
			scrollEasing:"easeOutQuint"
			/**autoDraggerLength:false*/
		});

		/** Fancybox Script */
		$('.fancybox').fancybox();
		$('.fancybox-media')
		.attr('rel', 'media-gallery')
		.fancybox({
			openEffect : 'none',
			closeEffect : 'none',
			prevEffect : 'none',
			nextEffect : 'none',
			arrows : false,
			helpers : {
				media : {},
				buttons : {}
			}
		}); /** fancybox-media ends*/
	});
	</script>
	<?php
	echo $_webnukes->kvalue($general_settings, 'footer_code');
}
